package com.ustc.py.learn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.mysql.cj.util.StringUtils;
import com.ustc.py.learn.constant.KnowledgeTypeEnum;
import com.ustc.py.learn.dao.KnowledgeDao;
import com.ustc.py.learn.dao.LearnBasicDao;
import com.ustc.py.learn.entity.Knowledge;
import com.ustc.py.learn.entity.KnowledgeVo;
import com.ustc.py.learn.service.KnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("learnBasicService")
public class KnowledgeServiceImpl extends ServiceImpl<LearnBasicDao, Knowledge> implements KnowledgeService {

    @Autowired
    private KnowledgeDao knowledgeDao;


/*    *//**
     * 获取知识：输入章节编号，返回文本知识实体类
     *
     * @param chapterId
     * @return
     *//*
    public KnowledgeEntity getKnowledgeByChapterId(int typeId, int chapterId) {
        return baseMapper.selectOne(new QueryWrapper<KnowledgeEntity>().eq("chapter_id", chapterId).eq("type", typeId));
    }*/

    /**
     * 根据知识类型和类型下章节号，查询知识ID，即主键knowledgeId
     *
     * @param knowledgeType
     * @param chapterIds
     * @return
     */
    @Override
    public List<Integer> getBatchKnowledgeIdsByKnowledgeTypeAndChapterIds(int knowledgeType, List<Integer> chapterIds) {
        if (chapterIds == null || chapterIds.size() == 0)
            return new ArrayList<>();

        return knowledgeDao.getKnowledgeIdsByKnowledgeTypeAndChapterIds(knowledgeType, chapterIds);
    }

    /**
     * 根据知识类型查询所有可用的菜单项
     *
     * @param knowledgeType
     * @return
     */
    @Override
    public List<KnowledgeVo> getKnowledgeMenuList(int knowledgeType) {
        List<Knowledge> knowledges = knowledgeDao.queryKnowledgesByType(knowledgeType);

        List<KnowledgeVo> menuList = knowledges.stream()
                .map(knowledge -> {
                    return KnowledgeVo.builder()
                            .knowledgeId(knowledge.getId())
                            .knowledgeType(knowledge.getType())
                            .knowledgeChapterId(knowledge.getChapterId())
                            .chapterName(knowledge.getChapterName())
                            .knowledgeTitle(knowledge.getTitle())
                            .text(knowledge.getText())
                            .build();
                })
                .collect(Collectors.toList());
        return menuList;
    }

    /**
     * 根据knowledgeId查询知识详情
     *
     * @param knowledgeId
     * @return
     */
    @Override
    public KnowledgeVo getKnowledge(int knowledgeId) {
        Knowledge knowledge = baseMapper.selectOne(new QueryWrapper<Knowledge>().eq("id", knowledgeId));
        KnowledgeVo knowledgeVo = KnowledgeVo.builder()
                .knowledgeId(knowledge.getId())
                .knowledgeType(knowledge.getType())
                .knowledgeChapterId(knowledge.getChapterId())
                .knowledgeTitle(knowledge.getTitle())
                .chapterName(knowledge.getChapterName())
                .text(knowledge.getText())
                .build();
        return knowledgeVo;
    }

    @Override
    public Knowledge getKnowledgeById(int id) {
        return getById(id);
    }

    @Override
    public List<KnowledgeVo> getChapter(Integer typeId, Integer chapterId) {
        List<Knowledge> list = query().eq("chapter_id", chapterId).eq("type", typeId).list();
        List<KnowledgeVo> res=new ArrayList<>();
        list.forEach(knowledge -> {
            KnowledgeVo knowledgeVo = BeanUtil.copyProperties(knowledge, KnowledgeVo.class);
            res.add(knowledgeVo);
        });
        return res;
    }

    /**
     * Knowledge表的通用模糊搜索
     * @param typeId  知识类型id  可参见 KnowledgeTypeEnum
     * @param keyword 搜索关键词  (在标题上做搜索)
     * @return Jackiehan is in charge.
     */
    @Override
    public List<KnowledgeVo> search(Integer typeId, String keyword) {
        LambdaQueryWrapper<Knowledge> queryWrapper = new LambdaQueryWrapper<>();

        // typeId 参数合法判断
        if(typeId != null && typeId >= KnowledgeTypeEnum.BASIC.getTypeId() && typeId <= KnowledgeTypeEnum.NUMPY.getTypeId()){
            queryWrapper.eq(Knowledge::getType, typeId);
        }

        // keyword参数合法判断
        if(!StringUtils.isNullOrEmpty(keyword) && !StringUtils.isEmptyOrWhitespaceOnly(keyword)){
            queryWrapper.like(Knowledge::getTitle, keyword);
        }

        // 转换
        List<Knowledge> knowledges = this.list(queryWrapper);
        List<KnowledgeVo> VoList = knowledges.stream()
                .map(knowledge -> {
                    return KnowledgeVo.builder()
                            .knowledgeId(knowledge.getId())
                            .knowledgeType(knowledge.getType())
                            .knowledgeChapterId(knowledge.getChapterId())
                            .chapterName(knowledge.getChapterName())
                            .knowledgeTitle(knowledge.getTitle())
                            .text(knowledge.getText())
                            .build();
                })
                .collect(Collectors.toList());
        return VoList;
    }

}
